#!/usr/bin/ruby
# -*- ruby -*-
#
# http://programmingpraxis.com/2013/07/23/telephone-lookup/

require 'pp'

DB = File.basename($0).gsub('.rb', '.input')

NUMBERS = File.open(DB).readlines.inject({}) do |r, l|
  name, number = l.strip.split(',')

  r.merge(name.upcase => number)
end

ARGV.each do |name|
  number = NUMBERS[name.upcase]

  puts name + ' : ' + (number || 'No match found')
end

=begin
# Code for creating the DB
DIGITS = {
  "a" => 2,
  "b" => 2,
  "c" => 2,
  "d" => 3,
  "e" => 3,
  "f" => 3,
  "g" => 4,
  "h" => 4,
  "i" => 4,
  "j" => 5,
  "k" => 5,
  "l" => 5,
  "m" => 6,
  "n" => 6,
  "o" => 6,
  "p" => 7,
  "q" => 7,
  "r" => 7,
  "s" => 7,
  "t" => 8,
  "u" => 8,
  "v" => 8,
  "w" => 9,
  "x" => 9,
  "y" => 9,
  "z" => 9,
}

def sign(str)
  return str.split('').map { |l| DIGITS[l.downcase].to_s }.join('')
end

File.open(DB).each_line do |line|
  line.strip!

  puts [line, sign(line)].join(',')
end
=end
